﻿using System;
using System.Windows.Controls;


namespace Sparkle.MVVM.ActionResults
{
    public abstract class ActionResult : IActionResult
    {
        public string ViewName { get; protected set; }

        public object DataContext { get; protected set; }

        protected ActionResult(string viewName, object dataContext)
        {
            ViewName = viewName;
            DataContext = dataContext;
        }

        public virtual void Execute()
        {
            if(Defaults.Shell!=null)
            {
                Defaults.Shell.HostingPanel.SetContent( GetView(ViewName));
            }
        }

        protected Control GetView(string viewName)
        {
            if (Defaults.RegisteredViews.ContainsKey(ViewName))
            {
                return (Control)Activator.CreateInstance(Defaults.RegisteredViews[ViewName]);
            }
            return new UserControl() {Content = new TextBlock() {Text = "View not found"}};
        }
    }
}
